package one.stand.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import one.stand.dto.*;
import one.stand.enums.SeeRecordEnums;
import one.stand.mapper.RecordPublicMapper;
import one.stand.mapper.SeeRecordPublicMapper;
import one.stand.model.SysDictValue;
import one.stand.service.RecordPublicService;
import one.stand.service.sys.SysDictValueService;
import one.stand.service.user.UserAdminService;
import one.stand.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@Service
public class RecordPublicServiceImpl implements RecordPublicService {
    @Autowired
    private SysDictValueService sysDictValueService;
    @Autowired
    private UserAdminService userAdminService;

    @Autowired
    private RecordPublicMapper recordPublicMapper;
    @Autowired
    private SeeRecordPublicMapper seeRecordPublicMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Long callCount(Integer userId, Integer cardId, Integer companyId) {
        return recordPublicMapper.selectCallCount(userId, cardId, companyId);
    }

    @Override
    public CollectionDto getCollection(Integer userId, Integer itemType, Integer itemId) {
        return recordPublicMapper.selectCollection(userId, itemType, itemId);
    }

    @Override
    public boolean isCollection(Integer userId, Integer itemType, Integer itemId) {
        CollectionDto collectionModel = getCollection(userId, itemType, itemId);
        return Objects.nonNull(collectionModel) && collectionModel.getIsDel() == 0;
    }

    @Override
    public Integer getCollectionCount(Integer itemType, Integer itemId) {
        PageHelper.startPage(1, 1);
        List<Integer> list = recordPublicMapper.selectCollectionUsers(itemType, itemId);
        return Math.toIntExact(((Page) list).getTotal());
    }

    @Override
    public List<Integer> getCollection(Integer itemType, Integer itemId) {
        return recordPublicMapper.selectCollectionUsers(itemType, itemId);
    }

    @Override
    public Integer formIdCount(Integer userId, int type) {
        return recordPublicMapper.selectFormIdCount(userId, type);
    }

    @Override
    public Long consultCount(Integer userId, Integer cardId, Integer companyId) {
        return recordPublicMapper.selectConsultCount(userId, cardId, companyId);
    }

    @Override
    public boolean isThumbs(Integer userId, Integer itemType, Integer itemId) {
        return recordPublicMapper.selectNums(userId, itemId, itemType) > 0;
    }

    @Override
    public int getThumbsCount(Integer itemType, Integer itemId) {
        return recordPublicMapper.selectNum(itemId, itemType);
    }

    @Override
    public String getThumbsNames(Integer itemType, Integer itemId) {
        return recordPublicMapper.selectName(itemId, itemType);
    }

    @Override
    public List<ThumbsDto> getThumbsImgs(Integer itemType, Integer itemId) {
        return recordPublicMapper.selectImg(itemId, itemType);
    }

    @Override
    public LiveRoomDto getLiveRoom(Integer roomId) {
        LiveRoomDto roomDto = recordPublicMapper.selectLiveRoom(roomId);
        SysDictValue sysDictValue = sysDictValueService.get(SysDictValueService.Type.LIVE_TYPE, String.valueOf(roomDto.getType()));
        if (Objects.nonNull(sysDictValue)) {
            roomDto.setTypeName(sysDictValue.getValue());
        }
        return roomDto;
    }

    @Override
    public String getLiveRoomWxUrl(Integer roomId) {
        LiveRoomDto liveRoomDto = getLiveRoom(roomId);
        return String.format(WECHAT_SHARE_ROOM, liveRoomDto.getCardId(), liveRoomDto.getCompanyId(), roomId);
    }

    @Override
    public Long countComment(Integer userId, Integer companyId) {
        return recordPublicMapper.selectCommentCount(userId, companyId);
    }

    @Override
    public Integer countCommentByDynamic(Integer dynamicId) {
        return recordPublicMapper.selectCommentCountByDynamic(0, dynamicId);
    }

    @Override
    public Integer countCommentByType(Integer type, Integer dynamicId) {
        return recordPublicMapper.selectCommentCountByDynamic(type, dynamicId);
    }

    @Override
    public List<CommentDto> getCommentList(Integer dynamicId, Integer limit) {
        return ConvertUtils.list(recordPublicMapper.selectCommentList(dynamicId, limit), dto -> {
            UserDto user = userAdminService.get(dto.getUserId());
            dto.setUserName(user.getName());
            dto.setUserLogo(user.getUserImg());

            if (Objects.nonNull(dto.getCoverUserId())) {
                UserDto userReply = userAdminService.get(dto.getCoverUserId());
                dto.setReplyName(userReply.getName());
            }
            return dto;
        });
    }

    @Override
    public List<CommentDto> getCommentList(Integer userId) {
        return recordPublicMapper.selectCommentListByUser(userId);
    }

    @Override
    public Long getPopularity(Integer code, Integer goodsId) {
        return recordPublicMapper.selectCountPopularity(code, goodsId);
    }

    @Override
    public void addPopularity(SeeRecordEnums seeRecordEnum, Integer goodsId) {
        SeeRecordDto seeRecordDto = seeRecordPublicMapper.select(seeRecordEnum.getCode(), goodsId);
        if (Objects.nonNull(seeRecordDto)) {
            seeRecordPublicMapper.updateCountPopularityAdd2(seeRecordDto.getId(), 1);
        } else {
            seeRecordPublicMapper.saveInit(seeRecordEnum.getCode(), goodsId, 1);
        }
    }
}
